import router from '@/router'
import store from '@/store'
// 前置守卫
router.beforeEach(async(to, from, next) => {
  const token = store.getters.token
  const url = to.path
  const whiteList = ['/login', '/404', '/test']

  if (token) {
    if (url === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.username) {
        await store.dispatch('user/getUserInfo')
        // 拿完数据之后, 进入页面之前, 就是触发路由权限筛选的位置
        const routes = await store.dispatch('permission/filterRoutes', store.state.user.userInfo.roles.menus)
        //   router.addRoutes(routes)
        // }
        router.addRoutes([
          ...routes,
          { path: '*', redirect: '/404', hidden: true }
        ])
        next(to.path)
        return
      }
      next()
    }
    // 未登录
    // 是否在白名单 在白名单放行
  } else {
    if (whiteList.includes(url)) {
      next()
    } else {
      next('/login')
    }
  }
})

